package cn.edu.cqut.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.edu.cqut.entity.Customer;
import cn.edu.cqut.entity.DivideModel;
import cn.edu.cqut.entity.ReportModel;
import cn.edu.cqut.service.ICustomerService;
import cn.edu.cqut.util.PageResult;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author FeBro-TuoMei
 * @since 2020-06-18
 */
@RestController
/*
 * 如果是Controller返回的就是jsp页面，加rest之后返回json文件
 */
@RequestMapping("/customer")
@CrossOrigin  //允许跨域请求 
public class CustomerController {

//	alt+? 就可以快捷打字
	@Autowired  
	private ICustomerService customerService;
	//返回一个客户列表
	/*
	 * @RequestMapping("/getCustomerList") public List<Customer>
	 * getCustomerList(Integer page,Integer limit){ return customerService.page(new
	 * Page<Customer>(page,limit)).getRecords(); // page分页对象 getRecords得到list列表 }
	 */
	@RequestMapping("/getCustomerList")
	public PageResult<Customer> getCustomerList(
			@RequestParam(defaultValue = "1") Integer page, //RequestParam设置默认请求
			@RequestParam(defaultValue = "10") Integer limit,
			Customer customer){
		QueryWrapper<Customer> queryWapper = new QueryWrapper<>();
		if(customer.getCusName()!= null) {
			// 模糊查询条件： 第一个是表里面的一个字段名
			queryWapper.like("cus_name", customer.getCusName());
		}
		Page<Customer> page3 = new Page<Customer>(page,limit);
		Page<Customer> page2 = customerService.page(page3,queryWapper);
		PageResult<Customer> pr = new PageResult<>();
		pr.setCode(0);
		pr.setCount(page2.getTotal()); //总数
		pr.setMsg("");
		pr.setData(page2.getRecords());		
		return pr;
		// page分页对象  getRecords得到list列表
	}
	//修改客户信息
	@RequestMapping("/updateCustomer")
	public PageResult<Customer> updateCustomer(Customer customer){
		customerService.updateById(customer);
		PageResult<Customer> pr = new PageResult<>();
		pr.setMsg("修改客户信息成功！");
		pr.setCode(0);	
		return pr;
	}
	//新增客户信息
	@RequestMapping("/addCustomer")
	public PageResult<Customer> addCustomer(Customer customer){
		customerService.save(customer);
		PageResult<Customer> pr = new PageResult<>();
		pr.setMsg("新增客户信息成功！");
		pr.setCode(0);	
		return pr;
	}
	//删除客户信息
	@RequestMapping("/delCustomer")  //添加前端映射
	public PageResult<Customer> delCustomer(Integer[] ids){
		customerService.removeByIds(Arrays.asList(ids));  //数组转list
		PageResult<Customer> pr = new PageResult<>();
		pr.setMsg("删除客户信息成功！");
		pr.setCode(0);	
		return pr;
	}
	
	@RequestMapping("/getCustomerCountByRegion")
	public Map<String , List<Object>> getCustomerCountByRegion(){
		//或去map里面的两个值
		List<Object> xList = new ArrayList<>();
		List<Object> sList = new ArrayList<>();
		
		List<ReportModel> customerCountByRegion = customerService.getCustomerCountByRegion();
		for(ReportModel rm : customerCountByRegion) {
			xList.add(rm.getRegion());
			sList.add(rm.getValue());
		}
		
		Map<String, List<Object>> ret = new HashMap<>();
		ret.put("x", xList);
		ret.put("s", sList);
		return ret;
	}
	
	@RequestMapping("/getCustomerWithContact")
	public List<Customer> getCustomerWithContact(){
		return customerService.getCustomerWithContact();
	}
	// 画echars图需要的参数
		@RequestMapping("/getCustomerBySearchInfo")
		public List<Object> getCustomerBySearchInfo(String searchInfo) {
			List<DivideModel> customerCountByLevel = customerService.getCustomerCountBySearchInfo(searchInfo);
			List<Object> res = new ArrayList<>();
			for(DivideModel rm : customerCountByLevel) {
				Map<String, Object> ret = new HashMap<>();
				ret.put("value", rm.getValue());
				ret.put("name", rm.getName()+"级客户");
				res.add(ret);
			}
			return res;
		}
		
		// 点击echars图根据等级，获取这个等级的客户全部数据
		@RequestMapping("/getCustomerByEcharsLev")
		public PageResult<Customer> getCustomerByEcharsLev(
				@RequestParam(defaultValue = "1") Integer page, //@RequestParam给参数设置默认值
				@RequestParam(defaultValue = "10") Integer limit,
				Customer customer) {
			QueryWrapper<Customer> queryWapper = new QueryWrapper<>();
			if (customer.getCusLevel() != null) {
				queryWapper.eq("cus_level", customer.getCusLevel());
			}
			if (customer.getCusCredit() != null) {
				queryWapper.eq("cus_credit", customer.getCusCredit());
			}
			if (customer.getCusSatisfaction() != null) {
				queryWapper.eq("cus_satisfaction", customer.getCusSatisfaction());
			}
			Page<Customer> page3 = new Page<Customer>(page,limit);
			Page<Customer> page2 = customerService.page(page3, queryWapper);
			PageResult<Customer> pr = new PageResult<>();
			pr.setCode(0);
			pr.setCount(page2.getTotal());
			pr.setMsg("");
			pr.setData(page2.getRecords());
			return pr;
		}
}

